/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.comsrv.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.annotation.Inner;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.comsrv.constant.ComConstant;
import com.ykht.proj.comsrv.entity.VSubscribeMsg;
import com.ykht.proj.comsrv.handle.SubscribeMsgHandler;
import com.ykht.proj.comsrv.service.VSubscribeMsgService;
import com.ykht.proj.comsrv.vo.SubscribeMsg;
import com.ykht.proj.comsrv.vo.SubscribeMsgActivityNotice;
import com.ykht.proj.comsrv.vo.SubscribeMsgCardExpire;
import com.ykht.proj.comsrv.vo.SubscribeMsgCouponExpire;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 订阅消息
 *
 * @author proj code generator
 * @date 2021-06-05 15:30:54
 */
@RestController
@AllArgsConstructor
@RequestMapping("/vsubscribemsg" )
@Api(value = "vsubscribemsg", tags = "订阅消息管理")
public class VSubscribeMsgController {

	private final WxMaService wxMaService;
	private final VSubscribeMsgService vSubscribeMsgService;

	@Inner(false)
	@GetMapping(value = "getActivityWillSendList")
	public R<List<VSubscribeMsg>> getActivityWillSendList(){
		List<VSubscribeMsg> list = new VSubscribeMsg().selectList(Wrappers.lambdaQuery(VSubscribeMsg.class)
				.eq(VSubscribeMsg::getSendFlag,0)
				.eq(VSubscribeMsg::getStatus,0)
				.eq(VSubscribeMsg::getType, ComConstant.SUBSCRIBE_MSG_ACTIVITY_NOTICE));
		return R.ok(list);
	}

	@ApiOperation(value = "订阅")
	@PostMapping("/subscribe" )
	public R subscribe(@RequestBody List<VSubscribeMsg> list) {
		list.stream().forEach(msg -> {
			if (msg.getType()!=null) msg.setType(msg.getType().toUpperCase());
			msg.setPersonId(SecurityUtils.getPersonId());
			msg.setCreateTime(LocalDateTime.now());
			msg.setCreateUser(SecurityUtils.getPersonId());
			msg.setSendFlag(0);
			msg.setStatus(0);
		});
		return R.ok(vSubscribeMsgService.saveBatch(list));
	}
	@ApiOperation(value = "卡过期提醒")
	@GetMapping("/sendSubscribeMsg1" )
	public R sendSubscribeMsg1(String templateId,String openId) throws WxErrorException {
		//获取微信小程序配置：
		WxMaSubscribeMessage var1  = new WxMaSubscribeMessage();
		List<WxMaSubscribeMessage.Data> list = new ArrayList<>();
		WxMaSubscribeMessage.Data thing1 = new WxMaSubscribeMessage.Data();
		thing1.setName("thing1");
		thing1.setValue("新人立减券");
		WxMaSubscribeMessage.Data amount2 = new WxMaSubscribeMessage.Data();
		amount2.setName("amount2");
		amount2.setValue("5");
		WxMaSubscribeMessage.Data thing3 = new WxMaSubscribeMessage.Data();
		thing3.setName("thing3");
		thing3.setValue("无门槛全场立减");
		WxMaSubscribeMessage.Data time4 = new WxMaSubscribeMessage.Data();
		time4.setName("time4");
		time4.setValue("2021年10月1日 15:01");
		list.add(thing1);
		list.add(amount2);
		list.add(thing3);
		list.add(time4);
		var1.setData(list);
		var1.setLang("zh_CN");
		var1.setPage("/pages/actives/active_list");
		var1.setMiniprogramState("formal");
		var1.setTemplateId(templateId!=null?templateId:"3z3L6qa5BMdbr1zHwAO6iCcz-W7Mx964X9dzwFp2l2A");
		var1.setToUser(openId!=null?openId:"o7xw-5NxoLCQgYCb3eLZkyKSzBU4");
		wxMaService.getMsgService().sendSubscribeMsg(var1);
		return R.ok();
	}
	@ApiOperation(value = "订阅消息-优惠券过期")
	@PostMapping("/sendSubscribeMsgCouponExpire")
	@Inner(false)
	public R sendSubscribeMsgCouponExpire(@RequestBody SubscribeMsgCouponExpire subscribeMsg)  {
		try {
			vSubscribeMsgService.sendSubscribeMsg(subscribeMsg);
			return R.ok();
		} catch (Exception e) {
//			e.printStackTrace();
			return R.failed(e.getMessage());
		}
	}
	@ApiOperation(value = "订阅消息-卡过期")
	@PostMapping("/sendSubscribeMsgCardExpire")
	@Inner(false)
	public R sendSubscribeMsgCardExpire(@RequestBody SubscribeMsgCardExpire subscribeMsg)  {
		try {
			vSubscribeMsgService.sendSubscribeMsg(subscribeMsg);
			return R.ok();
		} catch (Exception e) {
//			e.printStackTrace();
			return R.failed(e.getMessage());
		}
	}
	@ApiOperation(value = "订阅消息-活动提醒")
	@PostMapping("/sendSubscribeMsgActivityNotice")
	@Inner(false)
	public R sendSubscribeMsgActivityNotice(@RequestBody SubscribeMsgActivityNotice subscribeMsg)  {
		try {
			vSubscribeMsgService.sendSubscribeMsg(subscribeMsg);
			return R.ok();
		} catch (Exception e) {
//			e.printStackTrace();
			return R.failed(e.getMessage());
		}
	}

}
